package com.foreveross.action;

import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.foreveross.common.CommonUtil;
import com.foreveross.common.Constants;
import com.foreveross.service.ExpressService;
import com.foreveross.service.LogService;
import com.foreveross.util.AuthUtil;
import com.foreveross.vo.Express;
import com.foreveross.vo.SysRole;
import com.foreveross.vo.Tenement;
import com.foreveross.vo.common.PageVo;
import com.foreveross.vo.common.ResultVo;
import com.foreveross.vo.common.ResultVo;

@Controller
@RequestMapping(value = "/sysExpressController")
public class SysExpressController extends BaseCostroller {

	private Logger log = Logger.getLogger(SysExpressController.class);

	@Resource
	private ExpressService expressService;

	@Resource
	private LogService logService;
	
	
	/**
	 * 快递信息查询
	 * @param request
	 * @param response
	 * @param express_no
	 * @param house_user_name 业主姓名
	 * @param name 业主对应收件人对应姓名
	 * @param type
	 * @param status
	 * @param house_id
	 * @param OnePageCount
	 * @param currentPage
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/getExpress")
	public void getExpress(HttpServletRequest request,
			HttpServletResponse response, String express_no, String house_user_name,String name,
			String type, String status, String houseId, String page,String telephone,
			String rows,String userId,String resCode) {
		ResultVo resultVo = new ResultVo();
		try {
			int countRecord = (int) expressService.getExpressListCount(
					express_no,house_user_name, name, type, status, houseId,telephone,userId,resCode);
			PageVo<Express> pageObj = new PageVo<Express>(page, countRecord,rows);
			pageObj.setRows(expressService.findExpressList(express_no, house_user_name, name, type,
					status, houseId,telephone, pageObj.getpageIndex(),
					pageObj.getpageSize(),userId,resCode));
			resultVo.setData(pageObj);
			resultVo.setSuccess(true);
			log.info("快递信息 查看成功!");
		} catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage("快递信息  查看失败");
			log.error("快递信息 查看失败!", e);
		}
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}
	
	/**
	 * 快递信息添加修改
	 * @param request
	 * @param response
	 * @param express
	 * @param updateType
	 */
	@RequestMapping(method=RequestMethod.POST, value="/addOrUpdateExpress")
	public void addOrUpdateExpress(HttpServletRequest request,HttpServletResponse response,
			Express express,String updateType){    
		ResultVo resultVo = new ResultVo();
		try{
			expressService.saveOrUpdate(express,updateType,express.getId());
			resultVo.setSuccess(true);
			logService.saveLog(this.getUserIP(request),Constants.OPERATE_TYPE_BACKGROUND,updateType.equals(Constants.UPDATE_ADD)?Constants.LOG_ADD:Constants.LOG_UPDATE, "快递信息"+Constants.getUpdateType(updateType),
					AuthUtil.getUserid(request), 
					Constants.getUpdateType(updateType)+"快递信息[" + express.toString()
							+ "]");
			log.info("快递信息 "+updateType+"成功!");
		}catch(Exception e){
			resultVo.setSuccess(false);
			resultVo.setMessage("快递信息 "+updateType+"失败");
			log.error("快递信息"+updateType+"失败!",e);
		}
		
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}
	
	
	/**
	 * 查看快递详情
	 * 
	 * @param request
	 * @param response
	 * @param id
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/findExpress")
	public void findExpress(HttpServletRequest request,
			HttpServletResponse response, String id) {
		ResultVo resultVo = new ResultVo();
		try {
			Express express = (Express) this.findObject(
					 Express.class, id);
			resultVo.setSuccess(true);
			resultVo.setData(express);
		} catch (Exception e) {
			e.printStackTrace();
			resultVo.setSuccess(false);
			resultVo.setMessage("查看快递详情失败");
		}
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}
	
	
	/**
	 * 快递信息删除
	 * @param request
	 * @param response
	 * @param ids
	 */
	@RequestMapping(method=RequestMethod.POST, value="/deleteExpressList")
	public void deleteExpressList(HttpServletRequest request,HttpServletResponse response,
			String ids){    
		ResultVo resultVo = new ResultVo();
		try{
			expressService.deleteObjecList("Express", ids);
			resultVo.setSuccess(true);
			logService.saveLog(this.getUserIP(request),Constants.OPERATE_TYPE_BACKGROUND,Constants.LOG_DEL, "快递信息删除",
					AuthUtil.getUserid(request), 
					"快递信息[" + ids+ "]");
			log.info("快递信息 删除成功!");
		}catch(Exception e){
			resultVo.setSuccess(false);
			resultVo.setMessage("快递信息 删除失败");
			log.info("快递信息 删除失败!");
			e.printStackTrace();
		}
		
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}

}
